<template>
	<view class="authorizedEvent">
		<!-- <uni-nav-bar color="#000" background-color="#fff" :status-bar="true" left-icon="arrowleft" left-text="授权详情" title="授权详情" @clickLeft="backIndex" /> -->
		<view :class="isAccountBtn==false?'':'auth_content'">
			<uni-header
				:isAudit="isAudit"
				:statusList="statusData" 
				:type_id="typeID"
				:isProvince="isProvince"
				@toAudit="audit"
				@toAuthorized="authorized"
				@toSelectStatus='selectStatus'>
			</uni-header>
			
			<uni-authorizedCell
				v-if='isAudit==false'
				:authorizedList='authorizedData'
				@toAuthorizedDetail='toAuthorizedDetail'
				>
			</uni-authorizedCell>
			<uni-auditCell
				v-if="isAudit"
				:auditList='auditData'
				:typeID='typeID'
				class='authorizedcell'
				@toAuditDetail='toAuditDetail'
				>
				</uni-auditCell>
				<view class="nodata" v-if="isAudit==false&&authorizedData.length==0&&nodata">
					<view class="msfont ms-wushuju">			
					</view>
					<view class="">
						无数据
					</view>
				</view>
				<view class="nodata" v-if="isAudit&&auditData.length==0&&nodata">
					<view class="msfont ms-wushuju">			
					</view>
					<view class="">
						无数据
					</view>
				</view>
				<uni-load-more :status="loadingStatus" v-if='isAudiLoadingAll||isAuthLoadingAll' iconType='auto' :showIcon='showIcon'></uni-load-more>
		</view>
	</view>
</template>

<script>
	import uniHeader from '@/components/uni-event/uni-header.vue'
	import uniAuthorizedCell from '@/components/uni-event/uni-authorizedcell.vue';
	import {authorizedList,auditList} from "@/common/api.js";
	import uniAuditCell from '@/components/uni-event/uni-auditcell.vue';
	import uniLoadMore from "@/components/uni-load-more/uni-load-more.vue";
	import uniNavBar from "@/components/uni-nav-bar/uni-nav-bar.vue"
	export default{
		components:{
			uniHeader,
			uniAuthorizedCell,
			uniAuditCell,
			uniLoadMore,
			uniNavBar
		},
		data(){
			return{
				isAudit:false,
				statusList:[{type:"1",checkStatus:'待授权'},{type:"2",checkStatus:'未通过'},{type:"3",checkStatus:'已通过'}],  //授权赛事状态
				statusLists:[{type:"1",checkStatus:'待审核'},{type:'2',checkStatus:'未通过'},{type:'3',checkStatus:'已通过'}],  //审核赛事状态
				statusData:[],
				typeID:'1',
				auditData:[],
				loadMore:false,  //加载更多
				page:{
					pageSize:20,
					currentPage:1
				},
				typeID:"1",
				authorizedData:[],  //赛事列表
				loadMore:false,  //加载更多
				isLoading:false,//是否加载
				loadMores:false,  //加载更多
				isLoadings:false,//是否加载
				pages:{
					pageSize:20,
					currentPage:1,
				},
				auditData:[],
				isAuthLoadingAll:false,  //授权赛事加载所有
				isAudiLoadingAll:false,  //审核赛事加载所有
				loadTxt:"没有更多了",
				loadingStatus:"loading",
				isAccountBtn:true,  //是否展示切换按钮
				isProvince:false,  	//是否为省棋协
				showIcon:true,  //是否正在加载
				nodata:false
			}
		},
		mounted(){
			var _this=this;
			_this.isAuthLoadingAll=false;
			_this.isAudiLoadingAll=false;
			if(uni.getStorageSync("account")){
				var account=uni.getStorageSync("account");
				// console.log(account)
				if(account.accountnum==1){
					_this.isAccountBtn=false;
				}else{
					_this.isAccountBtn=true;
				}
				// console.log(account.level)
				if(Number(account.level)>1){
					_this.isProvince=false;
					_this.isAudit=false;
					// return;
				}else if(Number(account.level==1)){
					_this.isProvince=true;
				}
			}
			_this.authorized();
		},
		onload(e) {
			var _this=this;
			if(e.isAudit=="true"){
				_this.audit();
			}else{
				_this.authorized();
			}
		},
		//上拉加载
		onReachBottom(){
			var _this=this;
			if(_this.isAudit==false){
				if (_this.loadMore && !_this.isLoading) {
					_this.isLoading = true;
					var type='';
					switch(_this.typeID){
						case "1": type='wait';break;
						case "2": type='notpass';break;
						case "3": type='pass';break;
					}
					var data = {
						type:type,
						page: _this.page.currentPage,
						pageSize: _this.page.pageSize
					};
						authorizedList(data).then(function(res){
							var e=res;
							if(e.data.errcode==0){
								if (e.data.data.constructor == Array && e.data.data.length > 0) {
									_this.getAuthMoreCallback(e.data);
								}
							}else if(e.data.errcode==1000||e.data.errcode==1001){
								uni.removeStorageSync('token');
								uni.reLaunch({
									url:'/pages/home/welcome'
								})
							}else{
								uni.showToast({
									position: 'center',
									icon: 'none',
									title:e.data.msg
								});
							}
						});
				}
			}else{
				// console.log(_this.loadMores,_this.isLoadings)
				if (_this.loadMores && !_this.isLoadings) {
					var typeID=_this.typeID-1;
					var _this=this,data={
						state:Number(typeID),
						page:_this.pages.currentPage,
						pageSize:_this.pages.pageSize,
					};
					_this.isLoadings = true;
						auditList(data).then(function(res){
							var e=res;
							if(e.data.errcode==0){
								if (e.data.data.constructor == Array && e.data.data.length > 0) {
									// console.log(e.data)
									_this.getMoreCallbacks(e.data);
								}
							}else if(e.data.errcode==1000||e.data.errcode==1001){
								uni.removeStorageSync('token');
								uni.reLaunch({
									url:'/pages/home/welcome'
								})
							}else{
								uni.showToast({
									position: 'center',
									icon: 'none',
									title:e.data.msg
								});
							}
						});
				}
			}
		},
		//下拉刷新
		onPullDownRefresh(){
			var _this=this;
			if(_this.isAudit){
				_this.pages.currentPage=1;
				_this.auditData=[];
				_this.getAuditList();
				_this.loadMores=false;
				_this.isLoadings=false;
			}else{
				_this.page.currentPage=1;
				_this.authorizedData=[];
				_this.getAuthorizedList();
				_this.loadMore=false;
				_this.isLoading=false;
			}
			setTimeout(function () {
					uni.stopPullDownRefresh();  //停止下拉刷新动画
			}, 1000);
		},
		methods:{
			backIndex(){
				uni.redirectTo({
					url:"/pages/home/index"
				})
			},
			authorized(){
				var _this=this;
				_this.typeID='1';
				_this.isAudit=false;
				_this.loadMore=false;
				_this.isAuthLoadingAll=true;
				_this.statusData=_this.statusList;
				_this.page.currentPage=1;
				_this.authorizedData=[];
				_this.getAuthorizedList()
			},
			audit(){
				var _this=this;
				_this.typeID='1';
				_this.isAudit=true;
				_this.statusData=_this.statusLists;
				_this.pages.currentPage=1;
				_this.isAudiLoadingAll=true;
				_this.auditData=[];
				_this.getAuditList()
			},
			selectStatus(typeid){
				var _this=this;
				// console.log(typeid,_this.isAudit)
				_this.nodata=false;
				_this.isAuthLoadingAll=true;
				_this.isAudiLoadingAll=true;
				if(_this.isAudit==false){
					_this.typeID=typeid;
					_this.page.currentPage=1;
					_this.authorizedData=[];
					_this.getAuthorizedList();
				}else{
					_this.typeID=typeid;
					_this.pages.currentPage=1;
					_this.auditData=[];
					_this.getAuditList();
				}
			},
			getSign(btime,etime){
			  const _this = this;
			  if(btime && etime){
			    var b=new Date(btime.slice(0,19).replace(/-/g, "/")).getTime()
			    var e=new Date(etime.slice(0,19).replace(/-/g, "/")).getTime()
			    var now=new Date().getTime()
			    if(now<b){
			      return {text:'筹备中',class:'text-gray'}
			    }else if(b<=now && now<=e){
			      return {text:'报名中',class:'text-green'}
			    }else{
			      return {text:'已结束',class:'text-red'}
			    }
			  }else{
			    return {text:'筹备中',class:'text-gray'}
			  }
			},
			getRange (num) {
			  if (num === 58) return '全国';
			  if (num === 59) return '省级';
			  if (num === 60) return '县市级';
			  if (num === 61) return '地市级'
			},
			getOpText (isPlan, level, state , eventid) {
			  const str = isPlan + '' + level + state;
			  var opFLag=false;
			  switch (str) {
			    case '010':
			    case '110':
			    case '111':
			      return {txt:'去授权',flag:true};
			    case '121':
			    case '120':
			    case '131':
			    case '130':
			    case '141':
			    case '140':
			      return {txt:'立即申请',flag:true};
			  }
			  eventid?opFLag=true:opFLag=false;
			  return {txt:'进入赛事',flag:opFLag}
			},
		getSign(sbtime,setime,btime,etime){
		    var state = 0
		    if(btime && etime && sbtime && setime){
		        var sb=new Date(sbtime).getTime()
		        var b=new Date(btime).getTime()
		        var se=new Date(setime).getTime()
		        var e=new Date(etime).getTime()
		        var now=new Date().getTime()
		        if(now<sb){
		            state = 0   //报名开始前
		        }else if(sb<=now && now<=se){
		            state = 1   //报名中
		        }else if(se<now && now<b){
		            state = 2   //报名结束比赛开始前
		        }else if(b<=now && now<=e){
		            state = 3   //比赛中
		        }else{
		            state = 4   //比赛结束
		        }
		    }
		    return state
		},
			getCheckStata(state){
				switch(state){
					case "1":return "待授权";
					case '2': return "未通过";
					case "3":return "已授权";
					}
			},
			showNum(num,state,type){
			    var num_text = '';
			    var arr=['报名未开始','比赛未开始','筹备中','比赛中'];
			    switch(type){
						case 'pay_num':
								num_text=state!=0?(num>>0)+'人':'报名未开始';
								break;
						case 'play_num':
								num_text=(state==3 || state == 4)?(num>>0)+'人':arr[state];
								break;
						case 'wait_num':
								num_text=state==4?(num>>0)+'人':arr[state]
								break;
						case 'check_num':
								num_text=state==4?(num>>0)+'人':arr[state]
								break;
			    }
			    return num_text;
			},
			//授权赛事
			getAuthorizedList(){
				var _this=this,
				data={},type='';
				switch(_this.typeID){
					case "1": type='wait';break;
					case "2": type='notpass';break;
					case "3": type='pass';break;
				}
				data={
					type:type,
					page:_this.page.currentPage,
					pageSize:_this.page.pageSize
				}
				authorizedList(data).then(function(res){
					var e=res.data;
					// console.log(e)
					if(e.errcode==0){
						if(e.data.length==0){
							_this.nodata=true;
						}
						_this.initAuthDataCallback(e)
					}else if(e.errcode==1000||e.errcode==1001){
						uni.removeStorageSync('token');
						uni.reLaunch({
							url:'/pages/home/welcome'
						})
					}else{
						uni.showToast({
							position: 'center',
							icon: 'none',
							title:e.msg
						});
					}
				})
			},
			//授权赛事
			initAuthDataCallback(data) {
				var _this=this;
				_this.isAudiLoadingAll=false;
				data.data.map((e,i)=>{
					var timeReg=_this.getSign(e.game_start,e.game_end);
					var stateFlag=_this.getSign(e.reg_start,e.reg_end,e.game_start,e.game_end)
					_this.authorizedData.push({
						id: e.id,
						name: e.title,
						rank: _this.getRange(e.scale >> 0),
						money: e.bail === null ? 0 : (e.bail - 0).toFixed(2),
						isPlan: (e.is_plan >> 0),
						check_state:e.check_state,
						checkState: _this.getCheckStata(e.check_state),
						gameTime: [e.game_start, e.game_end],
						applyTime: [e.apply_start, e.apply_end],
						regTime:[e.reg_start,e.reg_end],
						intro: e.condition,
						org: e.num === null ? 0 : e.num,
						lswlName: e.lswl_event_title,
						// ssid: e.ssid,
						eventId: e.event_id === null ? false : e.event_id,
						op: _this.getOpText(e.is_plan >> 0, _this.$store.state.orgLevel, e.check_state >> 0,e.event_id).txt,
						opFlag:_this.getOpText(e.is_plan >> 0, _this.$store.state.orgLevel, e.check_state >> 0,e.event_id).flag,
						signtext:timeReg.text,
						signClass:timeReg.class,
						orgName: e.org_name,
						checkName: e.check_org_name === null ? '' : e.check_org_name,
						createTime: e.create_time,
						payNum : _this.showNum(e.pay_num,stateFlag,'pay_num'),
						playNum : _this.showNum(e.play_num,stateFlag,'play_num'),
						title:e.lswl_event_title || e.title,
						top_level_id: e.top_level_id === null ? '' :e.top_level_id ,
						scale: e.scale === null ? '' :e.scale ,
						version:e.version>>0,
						eventid:e.event_id,
						is_policy:e.policy_state==2 && parseInt(e.ms_price)>0,
						policy_num:e.policy ? e.policy : 0,
					})
				})
				_this.isAuthLoadingAll=Number(data.total)>_this.authorizedData.length?true:false;
			  _this.loadMore = Number(data.totalPage) > _this.page.currentPage;
			  _this.page.currentPage = _this.page.currentPage + 1;
			  _this.isLoading = false;
			},
			//加载更多授权赛事
			getAuthMoreCallback(data) {
			  var _this = this;
				_this.isAudiLoadingAll=false;
			  var list = [];
				data.data.map((e,i)=>{
					var timeReg=_this.getSign(e.game_start,e.game_end);
					var stateFlag=_this.getSign(e.reg_start,e.reg_end,e.game_start,e.game_end)
					list.push({
						id: e.id,
						name: e.title,
						rank: _this.getRange(e.scale >> 0),
						money: e.bail === null ? 0 : (e.bail - 0).toFixed(2),
						isPlan: (e.is_plan >> 0),
						check_state:e.check_state,
						checkState: _this.getCheckStata(e.check_state),
						gameTime: [e.game_start, e.game_end],
						applyTime: [e.apply_start, e.apply_end],
						regTime:[e.reg_start,e.reg_end],
						intro: e.condition,
						org: e.num === null ? 0 : e.num,
						lswlName: e.lswl_event_title,
						// ssid: e.ssid,
						eventId: e.event_id === null ? false : e.event_id,
						op: _this.getOpText(e.is_plan >> 0, _this.$store.state.orgLevel, e.check_state >> 0,e.event_id).txt,
						opFlag:_this.getOpText(e.is_plan >> 0, _this.$store.state.orgLevel, e.check_state >> 0,e.event_id).flag,
						signtext:timeReg.text,
						signClass:timeReg.class,
						orgName: e.org_name,
						checkName: e.check_org_name === null ? '' : e.check_org_name,
						createTime: e.create_time,
						payNum : _this.showNum(e.pay_num,stateFlag,'pay_num'),
						playNum : _this.showNum(e.play_num,stateFlag,'play_num'),
						title:e.lswl_event_title || e.title,
						top_level_id: e.top_level_id === null ? '' :e.top_level_id ,
						scale: e.scale === null ? '' :e.scale ,
						version:e.version>>0,
						eventid:e.event_id,
						is_policy:e.policy_state==2 && parseInt(e.ms_price)>0,
						policy_num:e.policy ? e.policy : 0
					})
				})
			  _this.authorizedData = _this.authorizedData.concat(list);
			  _this.loadMore = Number(data.totalPage) > _this.page.currentPage;
			  _this.page.currentPage = _this.page.currentPage + 1;
			  _this.isLoading = false;
				_this.isAuthLoadingAll=Number(data.total)>_this.authorizedData.length?true:false;
			},
			getCheckStata(state){
				switch(state){
					case "1":return "待审核";
					case '2': return "未通过";
					case "3":return "已通过";
					}
			},
			getAuditList(){
				var _this=this;
				var typeID=_this.typeID-1
				var _this=this,data={
					state:Number(typeID),
					page:_this.pages.currentPage,
					pageSize:_this.pages.pageSize,
				};
				auditList(data).then((res)=>{
					var e=res.data;
					// console.log(e)
					if(e.errcode==0){
						if(e.data.length==0){
							_this.nodata=true;
						}
						_this.initDataCallbacks(e)
						// console.log(e)
					}else if(e.errcode==1000||e.errcode==1001){
						uni.removeStorageSync('token');
						uni.reLaunch({
							url:'/pages/home/welcome'
						})
					}else{
						uni.showToast({
							position: 'center',
							icon: 'none',  
							title: e.msg
						});
					}
				})
			},
			initDataCallbacks(e) {
				var _this=this;
				_this.isAuthLoadingAll=false;
								// console.log(e)
				e.data.map((e,i)=>{
					var timeReg=_this.getSign(e.game_start,e.game_end);
					_this.auditData.push({
						id: e.examine_id,
						name: e.title,
						rank: _this.getRange(e.scale >> 0),
						money: e.bail === null ? 0 : (e.bail - 0).toFixed(2),
						isPlan: (e.is_plan >> 0),
						checkState: _this.getCheckStata(e.check_state),
						gameTime: [e.game_start, e.game_end],
						applyTime: [e.apply_start, e.apply_end],
						intro: e.condition,
						org: e.num === null ? 0 : e.num,
						lswlName: e.lswl_event_title,
						// ssid: e.ssid,
						eventId: e.event_id === null ? false : e.event_id,
						op: _this.getOpText(e.is_plan >> 0, _this.$store.state.orgLevel, e.check_state >> 0,e.event_id).txt,
						opFlag:_this.getOpText(e.is_plan >> 0, _this.$store.state.orgLevel, e.check_state >> 0,e.event_id).flag,
						signtext:timeReg.text,
						signClass:timeReg.class,
						orgName: e.org_name,
						checkName: e.check_org_name === null ? '' : e.check_org_name,
						createTime: e.create_time,
						payNum : e.pay_num === null? 0 : e.pay_num,
						playNum : e.play_num === null? 0 : e.play_num,
						title:e.lswl_event_title || e.title,
						top_level_id: e.top_level_id === null ? '' :e.top_level_id ,
						scale: e.scale === null ? '' :e.scale ,
						version:e.version>>0,
						eventid:e.event_id,
						is_policy:e.policy_state==2 && parseInt(e.ms_price)>0,
						policy_num:e.policy ? e.policy : 0,
						type:e.type
					})
				})
			  // _this.noData = !data.result.data;
			  _this.loadMores = Number(e.totalPage) > _this.pages.currentPage;
			  _this.pages.currentPage = _this.pages.currentPage + 1;
			  _this.isLoadings = false;
				_this.isAudiLoadingAll=Number(e.total) > _this.auditData.length?true:false;
								// console.log(e.total,_this.auditData.length,_this.isAudiLoadingAll,_this.aut)
			},
			getMoreCallbacks(data) {
			  var _this = this;
				_this.isAuthLoadingAll=false;
			  var list = [];
				// console.log(data)
				data.data.map((e,i)=>{
					var timeReg=_this.getSign(e.game_start,e.game_end);
					list.push({
						id: e.examine_id,
						name: e.title,
						rank: _this.getRange(e.scale >> 0),
						money: e.bail === null ? 0 : (e.bail - 0).toFixed(2),
						isPlan: (e.is_plan >> 0),
						checkState: _this.getCheckStata(e.check_state),
						gameTime: [e.game_start, e.game_end],
						applyTime: [e.apply_start, e.apply_end],
						intro: e.condition,
						org: e.num === null ? 0 : e.num,
						lswlName: e.lswl_event_title,
						// ssid: e.ssid,
						eventId: e.event_id === null ? false : e.event_id,
						op: _this.getOpText(e.is_plan >> 0, _this.$store.state.orgLevel, e.check_state >> 0,e.event_id).txt,
						opFlag:_this.getOpText(e.is_plan >> 0, _this.$store.state.orgLevel, e.check_state >> 0,e.event_id).flag,
						signtext:timeReg.text,
						signClass:timeReg.class,
						orgName: e.org_name,
						checkName: e.check_org_name === null ? '' : e.check_org_name,
						createTime: e.create_time,
						payNum : e.pay_num === null? 0 : e.pay_num,
						playNum : e.play_num === null? 0 : e.play_num,
						title:e.lswl_event_title || e.title,
						top_level_id: e.top_level_id === null ? '' :e.top_level_id ,
						scale: e.scale === null ? '' :e.scale ,
						version:e.version>>0,
						eventid:e.event_id,
						is_policy:e.policy_state==2 && parseInt(e.ms_price)>0,
						policy_num:e.policy ? e.policy : 0,
						type:e.type
					})
				})
			  _this.auditData = _this.auditData.concat(list);
			  _this.loadMores = Number(data.totalPage) > _this.pages.currentPage;
			  _this.pages.currentPage = _this.pages.currentPage + 1;
			  _this.isLoadings = false;
				_this.isAudiLoadingAll=Number(data.total) > _this.auditData.length?true:false
			},
			//授权详情
			toAuthorizedDetail(id){
				var _this=this;
				uni.navigateTo({
					url:"/pages/competitionAudit/eventDetail/authorizedDetail?id="+id+"&type="+_this.typeID
				})
			},
			//授权详情
			toAuditDetail(id){
				var _this=this;
				uni.navigateTo({
					url:"/pages/competitionAudit/eventDetail/auditDetail?id="+id+"&typeid="+_this.typeID
				})
			}
		}
	}
</script>

<style lang="less" scoped>
	.authorizedEvent{
		height: 100%;
		background: #F6F7F9;
		// position: relative;
	}
.authorizedEvent::-webkit-scrollbar {
		width: 0;
		height: 0;
		background-color: transparent;
	} 
	[v-cloak]{
	    display: none;
	}
	.auth_content{
		// margin-bottom: 40px;
	}
.nodata{
			position: fixed;
			top: 50%;
			left: 50%;
			margin-left: -117rpx;
			margin-top: -147rpx;
			.ms-wushuju{
				font-size: 150rpx;
				color: #ccc;
			}
			view:last-child{
				color: #aaa;
				text-align: center;
				margin-left: -20rpx;
				margin-top: -55rpx;
				font-size: 40rpx;
			}
		}
.footer{
	position: fixed;
	bottom: 10px;
	width: 100%;
	button{
		width: 80%;
		height: 32px;
		line-height: 32px;
		background: #1A66FF;
		border-radius: 22px;
	}
}
</style>
